/**
 * 流量线
 */
define(function(require) {

var util = require('./util')
var { trans2Path, createSvgPathElement } = util

//间隔30px,每次移2px
function animation(points, space = 2) {
  let distInterval = 30;
  let f = 0;

  return {
    get: function() {
      let offset = f;
      let arr = calcPoints(points, offset, distInterval);
      f += space;
      if (f === distInterval) {
        f = 0;
      }
      return arr;
    }
  }
}

function animationPlus(pointsObj, space = 2, count) {
  let distInterval = 30;
  let f = 0;
  let f2 = 0;

  var pathObj = trans2Path(pointsObj.points, pointsObj.type, pointsObj.scale, pointsObj.radius)
  var pathElement = createSvgPathElement(pathObj.pathString)
  var pathLength = pathElement.getTotalLength()

  if (count) {
    distInterval = Math.ceil(pathLength / count)
  } else {
    count = Math.ceil(pathLength / distInterval)
  }

  return {
    get: function() {
      let arr = [];
      for (var i = 0; i < count; i++) {
        let len = i * distInterval + f
        if (len < pathLength) {
          var point = pathElement.getPointAtLength(len);
          arr.push([point.x, point.y])
        }
      }
      f += space;
      if (f >= distInterval) {
        f = 0;
      }
      return arr;
    },
    getCluster: function getCluster() {
      const arr = []
      for (var i = 0; i < 40; i++) {
        let len = i * space + f2;
        if (len > pathLength) {
          len -= pathLength
        }
        const point = pathElement.getPointAtLength(len);
        arr.push([point.x, point.y])
      }
      f2 += space * 2;
      if (f2 >= pathLength) {
        f2 = 0;
      }

      return arr;
    }
  }
}
 
/**
 * 计算折线上的等距点
 * @param  {[type]} points   [折线]
 * @param  {[type]} offset   [起始偏移]
 * @param  {[type]} distInterval [流量点间隔]
 * @return {[type]}          [description]
 */
function calcPoints(points, offset, distInterval) {
  if (!points) {
    return [];
  }

  let arr = [];
  offset = offset || 0;
  for (let i = 1; i < points.length; i++) {
    let ret = calcPoint(points[i - 1], points[i], offset, distInterval)
    arr = arr.concat(ret.points);
    offset = distInterval - ret.leave;
  }

  return arr;
}

/**
 * 返回在startPoint和endPoint这根线上的等距点以及未填满的剩余长度
 * @param  {[type]} startPoint [起点]
 * @param  {[type]} endPoint   [终点]
 * @param  {[type]} offset     [起始偏移]
 * @param  {[type]} step       [两点步长]
 * @return {[type]}            {
 *                               points: [[],[]...],
 *                               leave: 0
 *                             }
 */
function calcPoint(startPoint, endPoint, offset, step) {
  let x = endPoint[0] - startPoint[0];
  let y = endPoint[1] - startPoint[1];
  let dis = Math.sqrt(x * x + y * y);
  if (dis < offset) {
    return {
      points: [],
      leave: step - offset + dis
    };
  }
  let count = parseInt((dis - offset) / step);
  let points = [];
  for (let i = 0; i <= count; i++) {
    points.push([startPoint[0] + ((offset + i * step) * x / dis), startPoint[1] + ((offset + i * step) * y / dis)]);
  }
  return {
    points: points,
    leave: dis - offset - step * count
  }
}


return { animation, animationPlus }
})
